package com.seaboxdata.interfaceJob;

import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.TransportationEnterprise;
import com.seaboxdata.entity.TransportationEnterpriseVo;
import com.seaboxdata.service.DpjfxtTokenService;
import com.seaboxdata.service.TransportationEnterpriseService;
import com.seaboxdata.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @create 2021-04-27 16:20
 **/
@Component
@Configuration
@Slf4j
public class DpjfxtTransportationEnterpriseJob {

    @Autowired
    private DpjfxtTokenService dpjfxtTokenService;

    @Autowired
    private TransportationEnterpriseService transportationEnterpriseService;

    @Value("${dpjfxt.transportation.enterprise.url}")
    private String url;

    @Value("${dpjfxt.transportation.enterprise.pageSize}")
    private String pageSize;

    @Value("${interface.isTest}")
    private String isTest;

    @Scheduled(cron = "${dpjfxt.transportation.enterprise.interval}")//每隔5分钟执行一次
    public void doCollect() {
        log.info("get transportation enterprise begin");
        //如果是测试,直接退出
        if ("true".equals(isTest)) {
            return;
        }
        String token = dpjfxtTokenService.getToken();
        String format = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String startTime = sdf.format(DateUtils.addDays(new Date(), -1)) + " 00:00:00";
        String endTime = sdf.format(new Date()) + " 00:00:00";

        Map<String, String> headerMap = new HashMap<>();
        headerMap.put("content-type", "application/json");
        if (StringUtils.isEmpty(token)) {
            log.error("get token error");
            return;
        }
        headerMap.put("token", token);
        handler(headerMap, Integer.valueOf(pageSize), 1, startTime, endTime);
    }

    public void handler(Map<String, String> headerMap, int pageSize, int pageNum, String startTime, String endTime) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNum", pageNum);
        paramMap.put("pageSize", pageSize);
        if (!StringUtils.isEmpty(startTime)) {
            paramMap.put("startTime", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            paramMap.put("endTime", endTime);
        }
        HttpUtils.Response response = HttpUtils.doPost(url, JSONObject.toJSONString(paramMap), "utf-8", headerMap);
        if (response.getStatusCode() == 200) {
            String result = response.getResponseData();
            TransportationEnterpriseVo transportationEnterpriseVo = JSONObject.parseObject(result, TransportationEnterpriseVo.class);
            if (transportationEnterpriseVo.getStatus() != 200) {
                log.error(transportationEnterpriseVo.getMessage());
            } else {
                //存入之前先将早前的数据清理掉,只保留当天的数据
                List<TransportationEnterprise> transportationEnterprises = transportationEnterpriseVo.getData().getRecords();
                if (!CollectionUtils.isEmpty(transportationEnterprises)) {
                    transportationEnterprises.forEach(x -> x.setCjsj(new Date()));
                    transportationEnterpriseService.saveBatch(transportationEnterprises);
                }

                if (pageNum < transportationEnterpriseVo.getData().getPages()) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler(headerMap, pageSize, pageNum + 1, startTime, endTime);
                }
            }
        }
    }
}
